package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyExam;
import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.model.enums.ExamChooseType;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.query.CategoryQuery;
import com.cd.javacxy.common.model.query.ExamQuery;
import com.cd.javacxy.common.model.vo.AjaxResponse;
import com.cd.javacxy.common.model.vo.CxyCategoryVo;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.base.BaseController;
import com.cd.javacxy.module.web.service.CxyCategoryService;
import com.cd.javacxy.module.web.service.CxyExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * CxyExam的路由接口服务
 *
 * @author yanghy
 */
@Controller
@RequestMapping("/exam")
@Slf4j
public class CxyExamController extends BaseController {

    /**
     * CxyExamService服务
     */
    private CxyExamService cxyExamService;

    private CxyCategoryService categoryService;

    @Autowired
    public void setCxyExamService(CxyExamService cxyExamService) {
        this.cxyExamService = cxyExamService;
    }

    @Autowired
    public void setCategoryService(CxyCategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("")
    public ModelAndView exam() {
        CategoryQuery categoryQuery = new CategoryQuery();
        categoryQuery.setShowInExam(Constants.Y);
        List<CxyCategoryVo> categoryList = this.categoryService.findCategoryList(categoryQuery);
        ModelAndView view = new ModelAndView("exam/exam");
        view.addObject("categoryList", categoryList);
        return view;
    }

    @ResponseBody
    @GetMapping("/examiers")
    public AjaxResponse<PageResult<CxyExam>> loadExamiers(ExamQuery examQuery) {
        PageResult<CxyExam> pageResult = this.cxyExamService.findExamUsers(examQuery);
        return AjaxResponse.<PageResult<CxyExam>>builder()
                .data(pageResult)
                .code(ResponseCode.SUCCESS.getCode())
                .build();

    }

    @GetMapping("/add")
    public ModelAndView addExam() {
        CategoryQuery categoryQuery = new CategoryQuery();
        categoryQuery.setShowInExam(Constants.Y);
        List<CxyCategoryVo> categoryList = this.categoryService.findCategoryList(categoryQuery);
        ModelAndView view = new ModelAndView("exam/addExam");
        view.addObject("categoryList", categoryList);
        view.addObject("examChooseType", ExamChooseType.values());
        return view;
    }

    @ResponseBody
    @PostMapping("/post/exam")
    public AjaxResponse<Object> postExam(HttpSession session, CxyExam exam, String[] answer, Integer[] rightAnswer) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        try {
            this.setUserBaseInfo(CxyExam.class, exam, session);
            this.cxyExamService.saveExam(exam, answer, rightAnswer);
            ajaxResponse.setData(exam);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (BussinessException e) {
            log.error("{}出题错误", exam.getUserName());
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
        } catch (Exception e) {
            log.error("{}出题错误", exam.getUserName());
            ajaxResponse.setErrorMsg("出题出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }

    @RequestMapping("/pre/do")
    public ModelAndView preDoExam(HttpSession session, Integer categoryId) {
        ModelAndView view = new ModelAndView("exam/doExam");
        view.addObject("categoryId", categoryId);
        return view;
    }

    @ResponseBody
    @GetMapping("/all")
    public AjaxResponse<List<CxyExam>> loadAllExam(HttpSession session, Integer categoryId) {
        AjaxResponse<List<CxyExam>> ajaxResponse = AjaxResponse.<List<CxyExam>>builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        try {
            List<CxyExam> list = this.cxyExamService.findExamListRand(categoryId);
            ajaxResponse.setData(list);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (Exception e) {
            ajaxResponse.setErrorMsg("加载试题出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
            log.error("{}加载试题出错", sessionUser.getUserName());
        }
        return ajaxResponse;
    }

    @ResponseBody
    @RequestMapping("/do/mark")
    public AjaxResponse<List<CxyExam>> doMark(HttpSession session, String examIds, String rightAnswers) {
        AjaxResponse<List<CxyExam>> ajaxResponse = AjaxResponse.<List<CxyExam>>builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        try {
            List<CxyExam> list = this.cxyExamService.doMark(examIds, rightAnswers);
            ajaxResponse.setData(list);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (BussinessException e) {
            log.error("{}改卷错误", sessionUser.getUserName());
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
        } catch (Exception e) {
            ajaxResponse.setErrorMsg("改卷出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
            log.error("{}改卷出错", sessionUser.getUserName());
        }
        return ajaxResponse;
    }
}
